using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Book : MonoBehaviour
{
    //private Vector3 BOOK_CENTER = new Vector3(-0.24f, 0f, 0f);
    private const int PAGES_NB = 5;
    
    private List<Page> pages;
    private Page currentPage;

    // Use this for initialization
    void Start()
    {
        pages = new List<Page>();

        Page page = null;

        for (int i = 0; i < PAGES_NB; ++i)
        {
            GameObject pageObject = (GameObject)Instantiate(Resources.Load("pagePrefab"));

            page = pageObject.GetComponent<Page>();
            page.setIndex(i);

            pages.Add(page);
        }

        currentPage = pages[0];

        //GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
        //sphere.transform.position = BOOK_CENTER;
        //sphere.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
        //sphere.active = false;
    }

    // Update is called once per frame
    void Update()
    {
        if (currentPage.getIsTurning()) return;
    }

    public void turnLeft()
    {
        if (currentPage.getIsTurning() || currentPage.getIsOnLeftSide()) return;

        currentPage.activeRotation(false);
    }

    public void turnRight()
    {
        if (currentPage.getIsTurning() || !currentPage.getIsOnLeftSide()) return;

        currentPage.activeRotation(true);
    }

    public void switchCurrentPage()
    {
        if (currentPage.getIsTurning()) return;

        if (currentPage.getIsOnLeftSide() )
        {
            currentPage = getNextPage();
        }
        else if( !currentPage.getIsOnLeftSide() )
        {
            currentPage = getPreviousPage();
        }
    }

    public Page getCurrentPage()
    {
        return currentPage;
    }

    private Page getNextPage()
    {
        return pages[(currentPage.getIndex() + 1) % PAGES_NB];
    }

    private Page getPreviousPage()
    {
        return pages[(currentPage.getIndex() - 1 + PAGES_NB) % PAGES_NB];
    }

}
